package cn.jiyun.controller;

import cn.jiyun.constant.MessageConstant;
import cn.jiyun.entity.Result;
import cn.jiyun.pojo.OrderSetting;
import cn.jiyun.serivce.OrderSettingService;
import cn.jiyun.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ordersetting")
public class OrdersettingControler {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("upload")
    public Result upload(MultipartFile excelFile){
        try {

            //1、解析出数据
            List<String[]> dataList = POIUtils.readExcel(excelFile);

            orderSettingService.importData(dataList);

            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("getDataByOrderDate")
    public Result getDataByOrderDate(String orderDate){
        try {

            List<Map<String,Object>> dataList =  orderSettingService.getDataByOrderDate(orderDate);

            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,dataList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("setNumberByOrderDate")
    public Result setNumberByOrderDate(String orderDate,Integer number){
        try {

            orderSettingService.setNumberByOrderDate(orderDate,number);

            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
